package com.ruoyi.workbench.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WbDept;
import com.ruoyi.workbench.constants.Constant;
import com.ruoyi.workbench.domain.WbLeafUnit;
import com.ruoyi.workbench.domain.WbWholeUnit;
import com.ruoyi.workbench.service.IWbLeafUnitService;
import com.ruoyi.workbench.utils.Generator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 叶子单元Controller
 *
 * @author hanjiafeng
 * @date 2021-02-24
 */
@Controller
@RequestMapping("/workbench/leaf_unit")
public class WbLeafUnitController extends BaseController {
    private final String prefix = "workbench/leaf_unit";
    private final IWbLeafUnitService wbLeafUnitService;

    public WbLeafUnitController(IWbLeafUnitService wbLeafUnitService) {
        this.wbLeafUnitService = wbLeafUnitService;
    }

    @RequiresPermissions("workbench:leaf_unit:view")
    @GetMapping()
    public String leafUnit() {
        return prefix + "/leaf_unit";
    }

    /**
     * 查询叶子单元列表
     */
    @RequiresPermissions("workbench:leaf_unit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest httpServletRequest) {
        startPage();

        WbWholeUnit wholeUnit = Generator.generateWholeUnit(httpServletRequest);

        List<WbWholeUnit> wbLeafUnitList = wbLeafUnitService.selectWbWholeUnitList(wholeUnit);
        return getDataTable(wbLeafUnitList);
    }

    @PostMapping("/selectChildrenUnitByParentId")
    @ResponseBody
    public List<WbLeafUnit> selectChildrenUnitByParentId(HttpServletRequest httpServletRequest) {
        Long parentId = null;
        try {
            parentId = Long.valueOf(httpServletRequest.getParameter("parentId"));
        } catch (NumberFormatException e) {
            logger.info("无单位id");
        }
        return wbLeafUnitService.selectChildrenUnitByParentId(parentId);
    }

    /**
     * 导出叶子单元列表
     */
    @RequiresPermissions("workbench:leaf_unit:export")
    @Log(title = "所辖村庄", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HttpServletRequest httpServletRequest) {
        WbWholeUnit wholeUnit = Generator.generateWholeUnit(httpServletRequest);
        List<WbWholeUnit> list = wbLeafUnitService.selectWbWholeUnitList(wholeUnit);
        ExcelUtil<WbWholeUnit> util = new ExcelUtil<>(WbWholeUnit.class);
        return util.exportExcel(list, "所辖村庄", ShiroUtils.getLoginName());
    }

    /**
     * 新增叶子单元
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit:add")
    @Log(title = "所辖村庄", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest httpServletRequest) {
        String unitId3 = httpServletRequest.getParameter("unitId3");
        String unitId4 = httpServletRequest.getParameter("unitId4");
        String unitId5 = httpServletRequest.getParameter("unitId5");
        String unitId6 = httpServletRequest.getParameter("unitId6");
        String unitName7 = httpServletRequest.getParameter("unitName7");
        //祖先id
        String ancestors = "100" + unitId3 + "," + unitId4 + "," + unitId5 + "," + unitId6;
        //父id
        long parentId = Long.parseLong(unitId6);
        WbLeafUnit wbLeafUnit = new WbLeafUnit();
        wbLeafUnit.setParentId(parentId);
        wbLeafUnit.setUnitName(unitName7);
        wbLeafUnit.setAncestorIds(ancestors);
        return toAjax(wbLeafUnitService.insertWbLeafUnit(wbLeafUnit));
    }

    /**
     * 修改叶子单元
     */
    @GetMapping("/edit/{unitId}")
    public String edit(@PathVariable("unitId") Long unitId, ModelMap modelMap) {
        WbLeafUnit wbLeafUnit = wbLeafUnitService.selectWbLeafUnitById(unitId);
        modelMap.put("wbLeafUnit", wbLeafUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit:edit")
    @Log(title = "所辖村庄", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WbLeafUnit wbLeafUnit) {
        return toAjax(wbLeafUnitService.updateWbLeafUnit(wbLeafUnit));
    }

    /**
     * 删除叶子单元
     */
    @RequiresPermissions("workbench:leaf_unit:remove")
    @Log(title = "所辖村庄", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wbLeafUnitService.deleteWbLeafUnitByIds(ids));
    }

    /**
     * 选择登录名领导最高级的部门
     *
     * @return 部门
     */
    @PostMapping("/selectHighestLevelDeptsByLoginName")
    @ResponseBody
    public WbDept selectHighestLevelDeptsByLoginName() {
        SysDept sysDept = ShiroUtils.getSysUser().getDept();
        //未获取到祖先时,退出重登
        if (StringUtils.isEmpty(sysDept.getAncestors())) {
            ShiroUtils.logout();
            return null;
        }
        WbDept wbDept = new WbDept();
        wbDept.setDept(sysDept);
        int level;
        if (sysDept.getDeptId() == Constant.ROOT_DEPT_ID) {
            level = 2;
        } else {
            level = Generator.getCommonCount(sysDept.getAncestors()) + 3;
        }
        wbDept.setLevel(level);
        return wbDept;
    }
}
